package com.ma.reportForm;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.ma.R;

import java.util.ArrayList;
import java.util.List;


import lomasky.xui.BaseViewHolder;
import lomasky.xui.ItemDivider;
import lomasky.xui.RecyclerViewAdapter;

/**
 * Created by loma on 2017/10/11.
 */

public class ChooseProjectDialog extends AlertDialog {
    private RecyclerViewAdapter<Project> adapte;

    public ChooseProjectDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(initView());

        setCanceledOnTouchOutside(true);
        Window window =   getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

    }

    private View initView( ) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_chosoe_project, null);

        ImageView close= (ImageView) view.findViewById(R.id.iv_close);
        close.setOnClickListener(view1 -> {
            dismiss();
        });

        RecyclerView recyclerView= (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new ItemDivider(this.getContext()));


        List<Project> list = new ArrayList<>();
        {
            Project p =new Project();
            p.setName("小区绿化项目1");
            list.add(p);
        }
        {
            Project p =new Project();
            p.setName("小区绿化项目2");
            list.add(p);
        }
        {
            Project p =new Project();
            p.setName("小区绿化项目3");
            list.add(p);
        }

        adapte = new RecyclerViewAdapter<Project>(this.getContext(),R.layout.project_item) {
            @Override
            public void onBind(BaseViewHolder holder, Project item, int position) {
                holder.setText(R.id.text,item.getName());
                holder.setRadioChecked(R.id.radioButton,item.isCheck());
                holder.getTextView(R.id.text).setTextColor(getContext().getResources().getColor(item.isCheck?R.color.colorPrimary:R.color.dark));
            }
        };
        adapte.setData(list);
        adapte.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setCheck(i==position);

                }
                adapte.notifyDataSetChanged();
            }
        });
        recyclerView.setAdapter(adapte);
        return  view;

    }

    class  Project{
        private  String name;
        private  boolean isCheck;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isCheck() {
            return isCheck;
        }

        public void setCheck(boolean check) {
            isCheck = check;
        }
    }
}
